package com.nus.fellowmark.common.filter.xss;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;

/**
 * Filter XSS code in HTML tags
 *
 * @author Ruoyi Chen
 * @email chenruoyisz@gmail.com
 * @create 2023/2/10
 */
public class XssUtil {

	/**
	 * Use the built-in basicWithImages whitelist
	 */
	private static final Whitelist WHITE_LIST = Whitelist.relaxed();

	/** Configure filtering parameters without formatting the code */
	private static final Document.OutputSettings OUTPUT_SETTINGS = new Document.OutputSettings().prettyPrint(false);
	static {
		// When editing rich text, some styles are implemented using style
		// such as red font style="color:red;"
		// So we need to add style attributes to all tags
		WHITE_LIST.addAttributes(":all", "style");
	}

	public static String clean(String content) {
		return Jsoup.clean(content, "", WHITE_LIST, OUTPUT_SETTINGS);
	}

}
